package net.zjitc.controller;

import net.zjitc.common.ServiceResponse;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Condition;
import net.zjitc.pojo.Product;
import net.zjitc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: KingCc
 * @Date: 2019/6/1 18:28
 */
@Controller
@RequestMapping("/manage/product")
public class adminProduct {
    @Autowired
    ProductService productService;

    @RequestMapping("/listProduct")
    public String findAll(Model model){
        List<Product> plist = productService.findAllProduct();
        List<Category> clist = productService.findAllCategory();
        model.addAttribute("plist",plist);
        model.addAttribute("clist",clist);
        return "/suyaoyajiu/product/list.jsp";
    }
    @RequestMapping("/addProductsView")
    public String addProductView(Model model){
        List<Category> clist = productService.findAllCategory();
        model.addAttribute("clist",clist);
        return "/suyaoyajiu/product/add.jsp";
    }
    @RequestMapping("/addProduct")
    @ResponseBody
    public ServiceResponse addProduct(Product product,
                                      @RequestParam(required = false,value = "file") MultipartFile file,
                                      HttpServletRequest request){
        return productService.addProduct(product,file,request);
    }
    @RequestMapping("/findProductById")
    public String findProductById(String id,Model model){
        Product list = productService.findProductById(id);
        List<Category> clist = productService.findAllCategory();
        model.addAttribute("p",list);
        model.addAttribute("clist",clist);
        return "/suyaoyajiu/product/edit.jsp";
    }
    @RequestMapping("/editProduct")
    @ResponseBody
    public ServiceResponse editProduct(Product product,
                                       @RequestParam(required = false,value = "file") MultipartFile file,
                                       HttpServletRequest request){
        return productService.editProduct(product,file,request);
    }
    @RequestMapping("/deleteProduct")
    public String deleteProduct(String id,Model model){
        int i = productService.deleteProduct(id);
        return "redirect:/manage/product/listProduct.do";
    }
    @RequestMapping("/searchByCondition")
    @ResponseBody
    public List<Product> searchByCondition(Condition condition){
        List<Product> plist = productService.searchByCondition(condition);
        return plist;
    }
}
